package com.health.db.dao.user.impl;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.health.db.dao.ServiceInterface;
import com.health.db.dao.user.ICourseShareLedgerDao;
import com.health.db.mapper.CourseShareLedgerMapper;
import com.health.db.model.CourseShareLedger;
import com.health.redis.RedisConstants;

/**
 * 用户分销账单表
 * @author YC
 *
 */
@Component("courseShareLedgerDao")
public class CourseShareLedgerDaoImpl extends ServiceInterface implements ICourseShareLedgerDao {

	@Autowired
	private  CourseShareLedgerMapper courseShareLedgerMapper;
	
	String  USERID=":userId:";

	public CourseShareLedgerMapper getCourseShareLedgerMapper() {
		return courseShareLedgerMapper;
	}

	public void setCourseShareLedgerMapper(
			CourseShareLedgerMapper courseShareLedgerMapper) {
		this.courseShareLedgerMapper = courseShareLedgerMapper;
	}

	@Override
	public CourseShareLedger selectByUserId(Long userId) {
		// TODO Auto-generated method stub
		CourseShareLedger  list =null;
		String key = RedisConstants.CourseShareLedgerMapper+USERID+userId.longValue();
		
		getRedisOperatiorLogic();
		Object object=null;
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			object=redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
		}
		if(null!=object )
		{
			if (!"".equals(object)) {
				list=(CourseShareLedger) object;
			}
		}
		else
		{
			list = courseShareLedgerMapper.selectByUserId(userId);
			
			if(redisOperatiorLogic.isJedisIsConnected())
			{
				redisOperatiorLogic.setEx(key, list, getOrderRedisGroupNo("1"));
			}
		}
		return list;
	}

	@Override
	public int insert(CourseShareLedger record) {
		// TODO Auto-generated method stub
		
		int result=courseShareLedgerMapper.insert(record);
		getRedisOperatiorLogic();
			
		String key = RedisConstants.CourseShareLedgerMapper+USERID+record.getAccountid().longValue();
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			redisOperatiorLogic.deleteObject(key,getOrderRedisGroupNo("1"));
		}
		
		return result;
	}


	@Override
	public int updateByPrimaryKeySelective(CourseShareLedger record) {
		// TODO Auto-generated method stub
		
		int result=courseShareLedgerMapper.updateByPrimaryKeySelective(record);
		
		getRedisOperatiorLogic();
		
		String key = RedisConstants.CourseShareLedgerMapper+USERID+record.getAccountid().longValue();
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			redisOperatiorLogic.deleteObject(key,getOrderRedisGroupNo("1"));
		}
		return result;
	}

}
